classSolution { public: intminimumCardPickup(vector<int>& nums){ int n = nums.size(); unordered_map<int, int> f; int ans = n + 1; for (int i = 0; i < n; i ++ ) { int cur = nums[i]; if (f.count(cur)) ans = min(ans, i - f[cur] + 1); // 更新哈希表 f[cur] = i; } if (ans == n + 1) ans = -1; return ans; } };
constint N = 210; classSolution { public: set<vector<int>> st[N]; intcountDistinct(vector<int>& nums, int k, int p){ int n = nums.size(); for (int i = 0; i < n; i ++ ) for (int j = i, cnt = 0; j < n; j ++ ) { if (nums[j] % p == 0) ++ cnt; if (cnt == k + 1) break; vector<int> cur(nums.begin() + i, nums.begin() + j + 1); int m = cur.size(); st[m].insert(cur); } int ans = 0; for (int i = 0; i < N; i ++ ) ans += st[i].size(); return ans; } };
constint N = 210; classSolution { public: set<vector<int>> st[N]; intcountDistinct(vector<int>& nums, int k, int p){ int n = nums.size(); for (int i = 0, j = 0, cnt = 0; i < n; i ++ ) { if (nums[i] % p == 0) ++ cnt; while (cnt > k) { if (nums[j] % p == 0) -- cnt; ++ j; } for (int k = j; k <= i; k ++ ) { vector<int> cur(nums.begin() + k, nums.begin() + i + 1); int m = cur.size(); st[m].insert(cur); } } int ans = 0; for (int i = 0; i < N; i ++ ) ans += st[i].size(); return ans; } };
using LL = longlong; constint N = 1e5 + 5, M = 26; classSolution { public: int L[N][M]; longlongappealSum(string s){ int n = s.size(); memset(L, -1, sizeof(L)); for (int i = 1; i <= n; i ++ ) { int cur = s[i - 1] - 'a'; for (int j = 0; j < M; j ++ ) L[i][j] = L[i - 1][j]; L[i][cur] = i; } LL ans = 0LL; for (int i = 1; i <= n; i ++ ) { int cur = s[i - 1] - 'a'; int left = L[i - 1][cur], right = n + 1; if (left == -1) left = 0; ans += 1ll * (i - left) * (right - i); } return ans; } };